package org.ssp.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ssp.dao.NewsDao;
import org.ssp.dao.StudyDao;
import org.ssp.dao.TravelDao;
import org.ssp.impl.NewsImpl;
import org.ssp.impl.StudyImpl;
import org.ssp.impl.TravelImpl;
import org.ssp.pojo.News;
import org.ssp.pojo.Study;
import org.ssp.pojo.Travel;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
 * News handle
 */
@WebServlet("/news")
public class NewsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String method=request.getParameter("method");
		/*if(request.getSession().getAttribute("assist")==null){//判断session是否失效
			request.getRequestDispatcher("/WEB-INF/page/admin/adm-login.jsp").forward(request, response);
			return;
		}*/
		if("page".equals(method)){//请求news list页
			request.getRequestDispatcher("/WEB-INF/page/admin/news.jsp").forward(request, response);
			
		}else if("list".equals(method)){//显示news列表
			System.out.println("news.....list.....");
			int page=Integer.valueOf(request.getParameter("page"));
			int rows=Integer.valueOf(request.getParameter("rows"));
			int offset=(page-1)*rows;
			int total=0;
			NewsDao newsd=new NewsImpl();
			ArrayList<News> list=newsd.findAll(offset, rows);
			total=newsd.findAll();
			JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("rows", JSONArray.fromObject(list,jsonConfig));
			json.element("total", total);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("list10".equals(method)){
			System.out.println("list10");
			int rows=10;
			int offset=0;
			NewsDao nd=new NewsImpl();
			ArrayList<News> list=nd.find10(offset,rows);
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			JSONObject json=new JSONObject();
			json.element("total", 10);
		    json.element("rows", JSONArray.fromObject(list,jsonConfig));
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}
		else if("add".equals(method)){//添加news
			System.out.println("news.....add.....");
		    String url=request.getParameter("firstimgurl");
			String title=request.getParameter("title");
			String content=request.getParameter("content");
            News news=new News();
			news.setTitle(title);
			news.setContent(content);
			news.setFirstimgurl(url);
			NewsDao newsd=new NewsImpl();
			newsd.addNews(news);//存入数据库
			JSONObject json=new JSONObject();
			json.element("success",true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
			
		}else if("delete".equals(method)){//删除news
			System.out.println("news....delete...");
		    String id=request.getParameter("id");
		    NewsDao newsd=new NewsImpl();
		    newsd.deleteNews(Integer.parseInt(id));
		    JSONObject json=new JSONObject();
		    json.element("success", true);
		    PrintWriter out=response.getWriter();
		    out.write(json.toString());
		    return;

		}else if("edit".equals(method)){//编辑news
			System.out.println("travel.....edit...");
			String url=request.getParameter("firstimgurl");
			String id=request.getParameter("id");
			String title=request.getParameter("title");
			String content=request.getParameter("content");
			String datetime=request.getParameter("datetime");
			System.out.println(id);
			System.out.println(url);
			System.out.println(title);
			System.out.println(content);
			System.out.println(datetime);
			News news=new News();
			news.setTitle(title);
			news.setContent(content);
			news.setFirstimgurl(url);
			news.setDatetime(new Date(datetime));
			news.setId(Integer.valueOf(id));
			NewsDao newd=new NewsImpl();
			newd.updateNews(news);
			JSONObject json=new JSONObject();
			json.element("success", true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;			
		}else if("item".equals(method)){
			String id=request.getParameter("id");
			News news =new News();
			NewsDao nd=new NewsImpl();
			news=nd.findById(Integer.valueOf(id));
			if(news!=null){
				request.setAttribute("obj", news);
			}
			request.getRequestDispatcher("/WEB-INF/page/admin/dashboard-item.jsp").forward(request, response);
		}else if("news-phone".equals(method)){//手机端news列表
			NewsDao newsdao=new NewsImpl();
			ArrayList<News> list=newsdao.findAllMobile();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			JSONObject json=new JSONObject();
		    json.element("news", JSONArray.fromObject(list,jsonConfig));
		    PrintWriter out=response.getWriter();
		    out.write(json.toString());
		    return;
		}else if("newsinfo".equals(method)){//手机端news详情
			System.out.println("news....newsinfo");
			int id=Integer.valueOf(request.getParameter("id"));
			NewsDao nd=new NewsImpl();
			String content=nd.getContent(id);
			JSONObject json=new JSONObject();
		    json.element("content", content);
		    PrintWriter out=response.getWriter();
		    out.write(json.toString());
		    return;
		}
		else if("mobile".equals(method)){
			String page=request.getParameter("page");
			if("news".equals(page)){
				NewsDao newsdao=new NewsImpl();
				ArrayList<News> list=newsdao.findAllMobile();
				request.setAttribute("newss", list);
				request.getRequestDispatcher("/WEB-INF/page/user/news.jsp").forward(request, response);
				
			}if("read".equals(page)){
				String id=request.getParameter("id");
				NewsDao newsdao=new NewsImpl();
				News news=newsdao.findById(Integer.parseInt(id));
				request.setAttribute("news",news);
				request.getRequestDispatcher("/WEB-INF/page/user/newsdetail.jsp").forward(request, response);
			}
		}
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
